$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;

    //搜索框 获取焦点
    $("#goods_id").focus();

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        window.location.href = "storekeeper.html";
    });

    //获取当前使用的无粘结定尺原料
    $.ajax({
        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
        type: "post",
        url: url + '/app/unbonded/getUnbondedDCMaterialForMachine',
        data: {},
        dataType: 'json',
        json: 'callback',
        success: function (res) {
            loading_hide();
            if (res.success) {
                showplandata(res);
            } else {
                console.log(res)
                $alert(res.message);
            }
        },
        error: function (error) {
            loading_hide();
            console.log(error);
            $alert(error.responseText);
        }
    });

    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            $("#goods_id").val("");
            if (!val) {
                $alert("请扫描定尺原料标签！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            } else {
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/unbonded/getUnbondedDCMaterial',
                    data: {
                        batch: val
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        pass = 1;
                        loading_hide();
                        if (res.success) {
                            var data = res.result;
                            $alert(val + "扫描成功");
                            //填充 当前机台与当前用料
                            showplandata(res);
                        } else {
                            console.log(res);
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        pass = 1;
                        loading_hide();
                        console.log(error);
                        $alert(error.responseText);
                    }
                });
            }
        }
    });

    function showplandata(res) {
        var data = res.result;
        $("section span.work_batch").html(data.batch);
        $("section span.work_name").html(data.inventoryName);
        $("section span.work_gg").html(data.inventorySpecification);
        $("section span.work_zl").html(data.availableQuantity);
        $("section span.work_dcCount").html(data.dcCount);
    }

    //点击清除  clear
    $(".bnt .clear").click(function () {
        var batch = $("section span.work_batch").html();
        if (!batch || batch == "空") {
            return $alert("请先扫描原料");
        }
        $.confirm("确认清除" + batch + "吗？", function () {
            if (pass == 0) return;
            pass = 0;
            loading_show("清除中...");
            //层卷 收线
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/unbonded/unbondedDCMaterialClear',
                data: {
                    batch: batch
                },
                timeout: 30000,
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    if (res.success) {
                        pass = 1;
                        $alert("原料清除成功！");
                        setTimeout(function () {
                            window.location.href = "unbondedDC.html";
                        }, 1200);
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

    //点击退料  return
    $(".content .return").on("click", function () {
        var batch = $("section span.work_batch").html();
        if (!batch || batch == "空") {
            $alert("请先扫描原料");
        }
        $(".mengceng").show();
        $("div.div_tuiliao").show();
        $("div.div_tuiliao input.syzl").val($("section span.work_zl").html()).focus();
        $("div.div_tuiliao input.batch").val(batch);
    });
    //点击 退料 取消
    $("div.div_tuiliao .tl_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_tuiliao").hide();
        $("#goods_id").focus();
    });
    //点击 退料 确定
    $("div.div_tuiliao .tl_queding").click(function () {
        if ($("div.div_tuiliao input.syzl").val() <= 0) {
            $alert("剩余重量应大于零");
            $("div.div_tuiliao input.syzl").focus();
        } else {
            //调用 收线 接口
            loading_show("退料中...");
            //拉丝 收线
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/unbonded/unbondedDCMaterialReturn',
                data: {
                    batch: $("div.div_tuiliao input.batch").val(),
                    aweight: $("div.div_tuiliao input.syzl").val()
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        loading_hide();
                        $alert("退料成功");
                        setTimeout(function () {
                            window.location.href = "unbondedDC.html";
                        }, 1200);
                    } else {
                        console.log(res);
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    console.log(error);
                    loading_hide();
                    $alert(error.responseText);
                }
            });
        }
    });

    //点击 完工
    $(".bnt .workover").click(function () {
        var batch = $("section span.work_batch").html();
        if (!batch || batch == "空") {
            $alert("请先扫描原料");
        }
        $(".mengceng").show();
        $("div.div_shouxian").show();
        $("div.div_shouxian input").val("");
        $("div.div_shouxian input.weight").val("").focus();
    });
    //点击 取消
    $("div.div_shouxian .sx_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_shouxian").hide();
        $("#goods_id").focus();
    });
    //点击 确定
    $("div.div_shouxian .sx_queding").click(function () {
        if (pass == 0) return
        pass = 0;
        if ($("div.div_shouxian input.weight").val() <= 0) {
            $alert("重量应大于零");
            $("div.div_shouxian input.weight").focus();
            pass = 1;
        } else if ($("div.div_shouxian input.length").val() <= 0) {
            $alert("米长应大于零");
            $("div.div_shouxian input.length").focus();
            pass = 1;
        } else {
            //调用 收线 接口
            loading_show("完成中...");
            //拉丝 收线
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/unbonded/unbondedDCComplete',
                data: {
                    batch: $("section span.work_batch").html(),
                    weight: $("div.div_shouxian input.weight").val(),
                    length: $("div.div_shouxian input.length").val()
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    if (res.success) {
                        loading_hide();
                        $alert("完成成功");
                        setTimeout(function () {
                            window.location.href = "unbondedDC.html";
                        }, 1200);
                        if (res.result) {
                            takeupPrint("unbondedDCTakeup", res.result);
                        }
                    } else {
                        console.log(res);
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    console.log(error);
                    loading_hide();
                    $alert(error.responseText);
                }
            });
        }
    });

});
